import request from '@/utils/request'
import {formatter} from '@/utils/formatter'

const getTestOption = cacheKey => {
  let resData = []
  return new Promise(resolve => {
    request({
      url: '/product/partner/list/1/100',
      method: 'get',
    }).then(res => {
      if (res.code === 200) {
        resData = formatter(res.data, 'productName', 'productCode')
        if (cacheKey) {
          sessionStorage.setItem(cacheKey, JSON.stringify(resData))
        }
      }
    }).finally(() => {
      resolve(resData)
    })
  })
}

const getRuzhuList = cacheKey => {
  let resData = [
    {
      label: '测试数据1',
      value: 'test1'
    },
    {
      label: '测试数据2',
      value: 'test2'
    },
  ]
  return new Promise(resolve => {
    request({
      url: '/product/partner/list/1/100',
      method: 'get',
    }).then(res => {
      if (res.code === 200) {
        resData = formatter(res.data, 'productName', 'productCode')
        if (cacheKey) {
          sessionStorage.setItem(cacheKey, JSON.stringify(resData))
        }
      }
    }).finally(() => {
      resolve(resData)
    })
  })
}

const filterApi = {
  getTestOption,
  getRuzhuList
}

export default filterApi
